package com.lsy.poi.cell;

import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;

import com.lsy.poi.Parser;
import com.lsy.poi.cell.extractor.AdaptableCellExtractor;
import com.lsy.poi.cell.extractor.CellExtractor;
import com.lsy.poi.cell.indexer.CellIndexer;
import com.lsy.poi.cell.indexer.FixedRangeCellIndexer;
import com.lsy.poi.cell.validator.CellValidator;

public class CellParser extends Parser {
	
	private int beginIndex;
	private int endIndex;
	
	private CellIndexer cellIndexer;
	
	/**
	 * cell 和 extractor、validators 之间是 "多对一" 的 关系，
	 * 即 "多个cell" 可以使用 "一个extractor、一组validators" 进行解析
	 */
	private XSSFCell[] 		cells;
	private Object[] 		cellValues;
	private CellExtractor 	extractor;
	private CellValidator[] validators;
	
	/** 默认的 提取器：AdaptableCellExtractor  */
	private final static CellExtractor 	 DEFAULT_EXTRACTOR  = new AdaptableCellExtractor();
	/** 默认的 验证器列表：空，即 不做任何验证  */
	private final static CellValidator[] DEFAULT_VALIDATORS = new CellValidator[]{};
	
	
	@Override
	public void spreadErrorHook(List<String> errors) {
		extractor.setErrors(errors);
		
		for (CellValidator cellValidator : validators) {
			cellValidator.setErrors(errors);
		}
	}
	
	
	
	/*--------------------------------------------
    |      			12 个  构造器			         |
    ============================================*/
	
	/* 8 个 不带 Indexer 的 构造器	 */
	public CellParser(int beginIndex, int endIndex, CellExtractor extractor, CellValidator[] validators) {
		/**
		 * 即使 提供了 beginIndex、endIndex，也走 Indexer 算法：默认 使用 FixedRangeCellIndexer(beginIndex, endIndex)
		 * 原因：为了在 parse() 的 时候 统一代码
		 */
		//this.beginIndex = beginIndex;  
		//this.endIndex = endIndex;
		
		this(new FixedRangeCellIndexer(beginIndex, endIndex), extractor, validators);
	}
	public CellParser(int index, CellExtractor extractor, CellValidator[] validators) {
		this(index, index, extractor, validators);
	}
	
	
	public CellParser(int beginIndex, int endIndex, CellValidator[] validators) {
		this(beginIndex, endIndex, DEFAULT_EXTRACTOR, validators);
	}
	public CellParser(int index, CellValidator[] validators) {
		this(index, index, validators);
	}
	
	
	public CellParser(int beginIndex, int endIndex, CellExtractor extractor) {
		this(beginIndex, endIndex, extractor, DEFAULT_VALIDATORS);
	}
	public CellParser(int index, CellExtractor extractor) {
		this(index, index, extractor);
	}
	
	
	public CellParser(int beginIndex, int endIndex) {
		this(beginIndex, endIndex, DEFAULT_EXTRACTOR, DEFAULT_VALIDATORS);
	}
	// index 是 "在当前 row 中选择 cell" 的 唯一途径，不能做成 缺省值
	public CellParser(int index) {
		this(index, index);
	}

	/* 4 个 带有 Indexer 的 构造器	 */
	public CellParser(CellIndexer cellIndexer, CellExtractor extractor, CellValidator[] validators) {
		super();
		this.beginIndex = -1;
		this.endIndex = -1;
		this.cellIndexer = cellIndexer;
		this.extractor = extractor;
		this.validators = validators;
	}
	public CellParser(CellIndexer cellIndexer, CellValidator[] validators) {
		this(cellIndexer, DEFAULT_EXTRACTOR, validators);
	}
	public CellParser(CellIndexer cellIndexer, CellExtractor extractor) {
		this(cellIndexer, extractor, DEFAULT_VALIDATORS);
	}
	public CellParser(CellIndexer cellIndexer) {
		this(cellIndexer, DEFAULT_EXTRACTOR, DEFAULT_VALIDATORS);
	}

	
	

	/**
	 * 核心方法
	 */
	public void parse() {
		
		cellValues = new Object[cells.length];
		
		int i = 0;
		
		for (XSSFCell cell : cells) {
			
			Object cellValue = extractor.extract(cell);
			cellValues[i++] = cellValue;
			
			//System.out.println(cellValue);
			
			for (CellValidator cellValidator : validators) {
				cellValidator.validate(cellValue, cell);
			}
		}
		
	}

	
	

	/*--------------------------------------------
    |      			getter、setter		         |
    ============================================*/
	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public int getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	public CellIndexer getCellIndexer() {
		return cellIndexer;
	}

	public void setCellIndexer(CellIndexer cellIndexer) {
		this.cellIndexer = cellIndexer;
	}

	public XSSFCell[] getCells() {
		return cells;
	}

	public void setCells(XSSFCell[] cells) {
		this.cells = cells;
	}

	
	public Object[] getCellValues() {
		return cellValues;
	}

	public void setCellValues(Object[] cellValues) {
		this.cellValues = cellValues;
	}

	public CellExtractor getExtractor() {
		return extractor;
	}


	public void setExtractor(CellExtractor extractor) {
		this.extractor = extractor;
	}


	public CellValidator[] getValidators() {
		return validators;
	}


	public void setValidators(CellValidator[] validators) {
		this.validators = validators;
	}
	
	
}
